<?

/**
* File operation helpers
*
* @version  1.0
* @author   adesanto_asman@yahoo.com
* @package  CGFile
*
*/

define('_DEFAULT_FILE_MODE'   , 0700);

/**
*
* @package CGFile
*
*/
class CGFile{

  /**
   * Handler of file
   *
   * @var unknown_type
   */
  public $fileHandle;

  /**
   * handler of Misc
   * Which error handler
   *
   * @var Misc
   */
  public $Misc;

  /**
   * row of content
   *
   * @var integer
   */
  public $row;

  /**
   * content of file
   *
   * @var array
   */
  public $buffer = array();

  /**
   * init error handler
   *
   * @param Misc $Misc
   */
  function initErrorHandler($Misc){

    $this->Misc = $Misc;
  }

  /**
  * creates File
  *
  * @param  string  $basePath    target path
  * @param  string  $filename
  * @param  boolean $checker
  *
  * @return boolean if $checker is true then if file exists
  *                 YES: returns failed
  *                 NO : returns to continue
  *
  */
  function createFile(&$basePath,
                      &$filename,
                       $checker){

      if($checker == true){

        /**
        * Checker true, if file exists tells error
        *
        */
        if(file_exists($basePath . $filename)){

          return false;
        }else{

          $this->fileHandle = fopen($basePath . $filename, "w");
          return true;
        }
      }else{

        if(file_exists($basePath . $filename)){

          /* Do nothing */
        }else{

          $this->fileHandle = fopen($basePath . $filename, "w");
        }

        return true;
      }
  }

  /**
   * open file
   *
   * @param string  $srcFilePath
   * @param string  $mode         the open file mode
   */
  function openFile($srcFilePath, $mode){

    $this->fileHandle = @fopen($srcFilePath, $mode);

    if($this->fileHandle){

      /* Do nothing*/
    }else{

      $Misc->usrDie("Error: File is not exist or path is invalid");
    }

    $this->row = 0;
  }

  /**
   * read the contents of file
   *
   */
  function readFile(){

    while(!feof($this->fileHandle)){

      $this->buffer[$this->row] = trim(fgets($this->fileHandle) ,"\n");

      $this->buffer[$this->row] = trim($this->buffer[$this->row],"\r");

      $this->row++;
    }
  }

  /**
   * get the read content
   *
   * @return array
   */
  function getReadContent(){

    return $this->buffer;
  }

  /**
   * get the read content row
   *
   * @return integer
   */
  function getReadContentRow(){

    return $this->row;
  }

  /**
  * write the File
  *
  * @param  string  $content
  *
  * @return integer false=FAILED
  *
  */
  function writeFile(&$content){

    return fwrite($this->fileHandle, $content);
  }

  /**
  * create directory
  *
  * @param  string  $dirName
  *
  * @return boolean false=FAILED
  *
  */
  function createDirectory(&$dirName){

    return @ mkdir( $dirName, _DEFAULT_FILE_MODE);
  }

  /**
  * Close fileHandle
  *
  * @param void
  *
  */
  function close(){

    if($this->fileHandle){

      fclose($this->fileHandle);
    }else{

      /* Do nothing */
    }
  }
}
?>